package com.paomi.client.activity.main;

import android.annotation.SuppressLint;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hannesdorfmann.mosby3.mvp.MvpView;
import com.paomi.client.MyApplication;
import com.paomi.client.R;
import com.paomi.client.activity.login.LoginActivity;
import com.paomi.client.activity.main.presenter.MainPresenter;
import com.paomi.client.base.BaseMvpActivity;
import com.paomi.client.fragment.classify.ClassifyFragment;
import com.paomi.client.fragment.first.FirstFragment;
import com.paomi.client.fragment.massage.MsgFragment;
import com.paomi.client.fragment.mine.MineFragment;
import com.paomi.client.utils.UIUtils;
import com.paomi.client.widget.RxToast;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseMvpActivity<MvpView, MainPresenter> {
    private Context context;
    private ViewPagerAdapter viewPagerAdapter;
    private MenuItem menuItem;
    private FirstFragment firstFragment;
    private ClassifyFragment classifyFragment;
    private MsgFragment msgFragment;
    private MineFragment mineFragment;

    @BindView(R.id.ll_home)
    LinearLayout ll_home;
    @BindView(R.id.ll_classify)
    LinearLayout ll_classify;
    @BindView(R.id.ll_massage)
    LinearLayout ll_massage;
    @BindView(R.id.ll_mine)
    LinearLayout ll_mine;
    @BindView(R.id.viewpager)
    ViewPager viewpager;

    @BindView(R.id.iv_home)
    TextView iv_home;
    @BindView(R.id.tv_home)
    TextView tv_home;
    @BindView(R.id.iv_classify)
    TextView iv_classify;
    @BindView(R.id.tv_classify)
    TextView tv_classify;
    @BindView(R.id.iv_massage)
    TextView iv_massage;
    @BindView(R.id.tv_massage)
    TextView tv_massage;
    @BindView(R.id.iv_mine)
    TextView iv_mine;
    @BindView(R.id.tv_mine)
    TextView tv_mine;


    private List<Fragment> list;
    private TabFragmentPagerAdapter adapter;


    @NonNull
    @Override
    protected int getLayoutId() {
        context = this;
        return R.layout.activity_main;
    }

    @Override
    protected String setTitle() {
        return "主页";
    }

    @Override
    protected void showBack(TextView button_goback) {

    }

    @Override
    protected void initEvent() {

        initFragment();
        MyApplication myApplication=(MyApplication) getApplication();
        myApplication.startService();

    }
    public void initFragment(){
        ll_home.setOnClickListener(mainListener);
        ll_classify.setOnClickListener(mainListener);
        ll_massage.setOnClickListener(mainListener);
        ll_mine.setOnClickListener(mainListener);

        //把Fragment添加到List集合里面
        list = new ArrayList<>();
        list.add(new FirstFragment());
        list.add(new ClassifyFragment());
        list.add(new MsgFragment());
        list.add(new MineFragment());
        adapter = new TabFragmentPagerAdapter(getSupportFragmentManager(), list);
        viewpager.setAdapter(adapter);
        //直接加载4个页面的数据
//        viewpager.setOffscreenPageLimit(4);
        viewpager.setCurrentItem(0);  //初始化显示第一个页面
        changeColor(iv_home, tv_home, R.color.main_color, getResources().getString(R.string.check_home));
        changeColor(iv_classify, tv_classify, R.color.text_gray_99, getResources().getString(R.string.uncheck_classify));
        changeColor(iv_massage, tv_massage, R.color.text_gray_99, getResources().getString(R.string.uncheck_msg));
        changeColor(iv_mine, tv_mine, R.color.text_gray_99, getResources().getString(R.string.uncheck_mine));
    }

    final View.OnClickListener mainListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.ll_home:
                    viewpager.setCurrentItem(0);
                    changeColor(iv_home, tv_home, R.color.main_color, getResources().getString(R.string.check_home));
                    changeColor(iv_classify, tv_classify, R.color.text_gray_99, getResources().getString(R.string.uncheck_classify));
                    changeColor(iv_massage, tv_massage, R.color.text_gray_99, getResources().getString(R.string.uncheck_msg));
                    changeColor(iv_mine, tv_mine, R.color.text_gray_99, getResources().getString(R.string.uncheck_mine));
                    break;
                case R.id.ll_classify:
                    viewpager.setCurrentItem(1);
                    changeColor(iv_classify, tv_classify, R.color.main_color, getResources().getString(R.string.check_classify));
                    changeColor(iv_home, tv_home, R.color.text_gray_99, getResources().getString(R.string.uncheck_home));
                    changeColor(iv_massage, tv_massage, R.color.text_gray_99, getResources().getString(R.string.uncheck_msg));
                    changeColor(iv_mine, tv_mine, R.color.text_gray_99, getResources().getString(R.string.uncheck_mine));
                    break;
                case R.id.ll_massage:
                    viewpager.setCurrentItem(2);
                    changeColor(iv_massage, tv_massage, R.color.main_color, getResources().getString(R.string.check_msg));
                    changeColor(iv_home, tv_home, R.color.text_gray_99, getResources().getString(R.string.uncheck_home));
                    changeColor(iv_classify, tv_classify, R.color.text_gray_99, getResources().getString(R.string.uncheck_classify));
                    changeColor(iv_mine, tv_mine, R.color.text_gray_99, getResources().getString(R.string.uncheck_mine));
                    break;
                case R.id.ll_mine:
//                    if (getUserInfo() == null) {
//                        getIntent(LoginActivity.class);
//                        return;
//                    }
                    viewpager.setCurrentItem(3);
                    changeColor(iv_mine, tv_mine, R.color.main_color, getResources().getString(R.string.check_mine));
                    changeColor(iv_home, tv_home, R.color.text_gray_99, getResources().getString(R.string.uncheck_home));
                    changeColor(iv_classify, tv_classify, R.color.text_gray_99, getResources().getString(R.string.uncheck_classify));
                    changeColor(iv_massage, tv_massage, R.color.text_gray_99, getResources().getString(R.string.uncheck_msg));
                    break;
            }
        }
    };

    private void changeColor(TextView iv, TextView tv, int color, String iv_icon) {
        iv.setText(iv_icon);
        iv.setTextColor(getResources().getColor(color));
        iv.setTypeface(iconfont);
        tv.setTextColor(getResources().getColor(color));
    }


    public class ViewPagerAdapter extends FragmentPagerAdapter {

        private List<Fragment> list;

        public void setList(List<Fragment> list) {
            this.list = list;
            notifyDataSetChanged();
        }

        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list != null ? list.size() : 0;
        }
    }

    @NonNull
    @Override
    public MainPresenter createPresenter() {
        return new MainPresenter();
    }

    public class TabFragmentPagerAdapter extends FragmentPagerAdapter {
        private FragmentManager mfragmentManager;
        private List<Fragment> mlist;

        public TabFragmentPagerAdapter(FragmentManager fm, List<Fragment> list) {
            super(fm);
            this.mlist = list;
        }

        @Override
        public Fragment getItem(int arg0) {
            return mlist.get(arg0);//显示第几个页面
        }

        @Override
        public int getCount() {
            return mlist.size();//有几个页面
        }
    }

    private long mExitTime = 0;

    @Override
    public void onBackPressed() {
        //1.点击的时间差如果大于2000，则提示用户点击两次退出
        if (System.currentTimeMillis() - mExitTime > 2000) {
            //2.保存当前时间
            mExitTime = System.currentTimeMillis();
            //3.提示
            RxToast.info("再按一次退出程序");
        } else {
            //4.点击的时间差小于2000，调用父类onBackPressed方法执行退出。
            super.onBackPressed();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //1.点击返回键条件成立
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN
                && event.getRepeatCount() == 0) {
            //2.点击的时间差如果大于2000，则提示用户点击两次退出
            if (System.currentTimeMillis() - mExitTime > 2000) {
                //3.保存当前时间
                mExitTime = System.currentTimeMillis();
                //4.提示
                RxToast.info("再按一次退出程序");
            } else {
                //5.点击的时间差小于2000，退出。
                finish();
                System.exit(0);
            }
        }
        return true;
    }

    private Intent homeIntent;

    @Override
    protected void onNewIntent(Intent intent) {
        homeIntent = intent;
        super.onNewIntent(intent);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (homeIntent != null) {
            if(homeIntent.getStringExtra("mainId")!=null){
                int id = homeIntent.getIntExtra("id", 0);
                switch (id) {
                    case 0:
//                    showFragment(0);//显示指定的fragment
                        viewpager.setCurrentItem(0);
                        changeColor(iv_home, tv_home, R.color.main_color, getResources().getString(R.string.check_home));
                        changeColor(iv_classify, tv_classify, R.color.text_gray_99, getResources().getString(R.string.uncheck_classify));
                        changeColor(iv_massage, tv_massage, R.color.text_gray_99, getResources().getString(R.string.uncheck_msg));
                        changeColor(iv_mine, tv_mine, R.color.text_gray_99, getResources().getString(R.string.uncheck_mine));
                        break;
                    case 1:
//                    showFragment(1);//显示指定的fragment
                        viewpager.setCurrentItem(1);
                        changeColor(iv_classify, tv_classify, R.color.main_color, getResources().getString(R.string.check_classify));
                        changeColor(iv_home, tv_home, R.color.text_gray_99, getResources().getString(R.string.uncheck_home));
                        changeColor(iv_massage, tv_massage, R.color.text_gray_99, getResources().getString(R.string.uncheck_msg));
                        changeColor(iv_mine, tv_mine, R.color.text_gray_99, getResources().getString(R.string.uncheck_mine));
                        break;
                    case 2:
//                    showFragment(2);//显示指定的fragment
                        viewpager.setCurrentItem(2);
                        changeColor(iv_massage, tv_massage, R.color.main_color, getResources().getString(R.string.check_msg));
                        changeColor(iv_home, tv_home, R.color.text_gray_99, getResources().getString(R.string.uncheck_home));
                        changeColor(iv_classify, tv_classify, R.color.text_gray_99, getResources().getString(R.string.uncheck_classify));
                        changeColor(iv_mine, tv_mine, R.color.text_gray_99, getResources().getString(R.string.uncheck_mine));
                        break;
                    case 3:
//                    showFragment(3);//显示指定的fragment
                        viewpager.setCurrentItem(3);
                        changeColor(iv_mine, tv_mine, R.color.main_color, getResources().getString(R.string.check_mine));
                        changeColor(iv_home, tv_home, R.color.text_gray_99, getResources().getString(R.string.uncheck_home));
                        changeColor(iv_classify, tv_classify, R.color.text_gray_99, getResources().getString(R.string.uncheck_classify));
                        changeColor(iv_massage, tv_massage, R.color.text_gray_99, getResources().getString(R.string.uncheck_msg));
                        break;
                }
            }

        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        homeIntent=null;
    }

    public void showFragment(int position) {
        hideFragments();
        Fragment fragment = list.get(position);
        android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.show(fragment);
        ft.commit();
    }

    public void hideFragments() {
        android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        for (Fragment fragment : list) {
            if (fragment != null) {
                ft.hide(fragment);
            }
        }
        ft.commit();
    }
}
